home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / misc / torus.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  77 lines

  1. ; $Id: torus.pro,v 1.3 1997/01/15 04:21:02 ali Exp $
  2. ;
  3. ; Copyright (c) 1988-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. pro torus, r, r0, verts, polys  ;Draw a torus, centered about (0,0,0).
  7. ; Major circle lies in xy plane.
  8. ; r = major radius, r0 = radius in Z direction.
  9. ; Verts = current vertex list, a (3,n) array of (x,y,z) coordinates.
  10. ; Polys = Polygon list.
  11. ; The vertices and polygons are appended to these lists.
  12.  
  13. n = 15            ;# of segments/circle
  14. f = 360./n * !dtor
  15.  
  16. old_verts = verts
  17. old_polys = polys
  18.  
  19. vy = sin(findgen(n)*f)    ;Y coordinates of circle
  20. vx = cos(findgen(n)*f)    ;X coordinates
  21.  
  22. kv = n_elements(old_verts)/3    ;Size of old vertex list
  23. verts = fltarr(3,kv + n^2)    ;New vertex list
  24. if kv gt 0 then verts[0,0] = old_verts    ;Append to old?
  25. kp = n_elements(old_polys)/4    ;Same with polygons
  26. polys = intarr(4,2*n^2+kp)
  27. if kp gt 0 then polys[0,0] = old_polys
  28.  
  29. ;    Draw vertices & edges in counter clockwise direction.
  30. for i=0,n-1 do begin
  31.     i1 = (i+ 1) mod n    ;Polygon between segments (i,i1)
  32.     cx = vx[i]*r        ;X around major circle
  33.     cy = vy[i]*r        ;Y
  34.     for j=0,n-1 do begin    ;Minor circle
  35.         j1 = (j+1) mod n  ;Polygon between segments (j,j1)
  36.         rr = vx[j]*r0    ;Radius of minor circle
  37.                 ;Translate
  38.         v = [ rr*vx[i] + cx, rr*vy[i] + cy, vy[j]*r0, 1] # !p.t
  39.         verts[0,i*n+j+kv] = v[0:2]
  40.         polys[0,kp] =  [3,kv+i*n+j,kv+i1*n+j,kv+i1*n+j1]
  41.         polys[0,kp+1] = [3,kv+i*n+j,kv+i1*n+j1, kv+i*n+j1]
  42.         kp = kp + 2
  43.         endfor
  44.     endfor
  45. return
  46. end
  47.  
  48.  
  49.  
  50. t3d,/reset        ;Reset transformation to identity
  51. verts = 0        ;Init arrays to empty
  52. polys = 0
  53. torus,.7,.25,verts, polys    ;1st torus fits into the cube (-1,1),(-1,1)
  54.                 ;  and (-1,1) approximately.
  55. t3d,ro=[90,0,0]        ;Rotate next by 90 about x
  56. t3d,tr=[.7,0,0]        ;and move it to right by .7
  57. torus,.7,.25,verts, polys    ;2nd torus
  58.  
  59. vmin = fltarr(3) & vmax = vmin    ;get min & max of each coordinate.
  60. for i=0,2 do begin
  61.     v = verts[i,*]
  62.     vmin[i] = min(v)
  63.     vmax[i] = max(v)
  64.     endfor
  65. !x.s = [-vmin[0],.9]/(vmax[0]-vmin[0]) ;Set up data scaling to normalized 
  66. ;                coordswhich is the cube [0,1],[0,1],[0,1]
  67. !y.s = [-vmin[1],.9]/(vmax[1]-vmin[1])
  68. !z.s = [-vmin[2],.9]/(vmax[2]-vmin[2])
  69.  
  70. surfr,ax=45        ;view from 45 degree orientation
  71. erase
  72. ;  Monochrome?
  73. if !d.n_colors le 2 then top = 255 else top = !d.n_colors -1 < 255
  74. b = polyshade(verts,polys,/t3d,/data, xsize=512,ysize=512, top=top)
  75. tv,b
  76. end
  77.